//+------------------------------------------------------------------+
//| 五浪理论遗传算法EA
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

// 包含必要的头文件
#include "WaveTheory_GA_Logger.mqh"
#include "WaveTheory_Enhanced_Logging.mqh"

// 输入参数
input group "=== 基本设置 ==="
input bool   EnableTrading = true;        // 启用交易
input double LotSize = 0.1;               // 交易手数
input int    StopLoss = 100;              // 止损点数
input int    TakeProfit = 200;            // 止盈点数
input bool   UseTrailingStop = true;      // 使用追踪止损
input int    TrailingStop = 50;           // 追踪止损点数
input int    TrailingStep = 10;           // 追踪步长

input group "=== 波浪理论设置 ==="
input bool   EnableWaveTheory = true;     // 启用波浪理论
input int    WaveLookback = 100;          // 波浪回看周期
input double MinWaveAmplitude = 0.0001;   // 最小波浪幅度
input double MaxWaveRetracement = 0.786;  // 最大回撤比例
input double MinWaveExtension = 1.0;      // 最小延伸比例

input group "=== 遗传算法设置 ==="
input bool   EnableGA = true;             // 启用遗传算法
input int    GAPopulationSize = 30;       // 种群大小
input int    GAGenerations = 50;          // 代数
input double GAMutationRate = 0.15;       // 变异率
input double GACrossoverRate = 0.8;       // 交叉率
input int    GAMinTrades = 10;            // 最小交易次数
input double GAMinWinRate = 0.4;          // 最小胜率
input double GAMinProfitFactor = 1.2;     // 最小盈亏比
input int    GAMaxDrawdown = 20;          // 最大回撤(%)

input group "=== AI模式设置 ==="
input ENUM_AI_MODE AI_Mode = AI_MODE_RUN;     // AI运行模式
input bool   EnableAIOptimization = false;     // 启用AI优化
input int    AITrainingInterval = 24;          // AI训练间隔(小时)
input bool   UseLowResourceMode = true;        // 使用低资源模式

input int MinConsecutiveBars = 3; // 最小连续同向K线数（3+2n）；

// 添加AI模式枚举
enum ENUM_AI_MODE {
    AI_MODE_TRAIN,    // 训练模式
    AI_MODE_RUN,      // 运行模式
    AI_MODE_HYBRID    // 混合模式
};

// 添加AI配置结构
struct AI_Config {
    int populationSize;        // 种群大小
    int maxGenerations;        // 最大代数
    double mutationRate;       // 变异率
    double crossoverRate;      // 交叉率
    int minTrades;            // 最小交易次数
    double minWinRate;        // 最小胜率
    double minProfitFactor;   // 最小盈亏比
    int maxDrawdown;          // 最大回撤
    int lookbackPeriod;       // 回看周期
    bool useGPU;              // 使用GPU
    int threadCount;          // 线程数
};

// 全局变量
int g_waveHandle = INVALID_HANDLE;        // 波浪指标句柄
int g_gaHandle = INVALID_HANDLE;          // 遗传算法句柄
datetime g_lastBarTime = 0;               // 最后一根K线时间
bool g_isNewBar = false;                  // 是否新K线
AI_Config g_aiConfig;         // AI配置
datetime g_lastTrainingTime;  // 上次训练时间
bool g_isTraining;           // 是否正在训练

//+------------------------------------------------------------------+
//| Expert initialization function
//+------------------------------------------------------------------+
int OnInit() {
    // 初始化日志系统
    InitWaveTheoryLogger(true, true, true, true, true);
    LogMessage(LOG_INFO, "=== EA初始化开始 ===");
    
    // 检查输入参数
    if(!ValidateInputs()) {
        LogError("OnInit", "输入参数验证失败");
        return INIT_PARAMETERS_INCORRECT;
    }
    
    // 初始化AI配置
    InitializeAIConfig();
    
    // 初始化波浪指标
    if(EnableWaveTheory) {
        g_waveHandle = iCustom(_Symbol, PERIOD_CURRENT, "WaveTheory_GA_Indicator");
        if(g_waveHandle == INVALID_HANDLE) {
            LogError("OnInit", "波浪指标初始化失败");
            return INIT_FAILED;
        }
        LogMessage(LOG_INFO, "波浪指标初始化成功");
    }
    
    // 初始化遗传算法
    if(EnableGA) {
        g_gaHandle = iCustom(_Symbol, PERIOD_CURRENT, "WaveTheory_GA_Optimizer");
        if(g_gaHandle == INVALID_HANDLE) {
            LogError("OnInit", "遗传算法初始化失败");
            return INIT_FAILED;
        }
        LogMessage(LOG_INFO, "遗传算法初始化成功");
    }
    
    // 记录初始化完成信息
    LogMessage(LOG_INFO, "EA初始化完成");
    LogMessage(LOG_INFO, StringFormat("AI模式: %s", EnumToString(AI_Mode)));
    LogMessage(LOG_INFO, StringFormat("低资源模式: %s", UseLowResourceMode ? "启用" : "禁用"));
    
    return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| 初始化AI配置
//+------------------------------------------------------------------+
void InitializeAIConfig() {
    // 根据运行模式设置配置
    switch(AI_Mode) {
        case AI_MODE_TRAIN:
            // 训练模式 - 使用高配置
            g_aiConfig.populationSize = 500;
            g_aiConfig.maxGenerations = 5000;
            g_aiConfig.mutationRate = 0.15;
            g_aiConfig.crossoverRate = 0.8;
            g_aiConfig.minTrades = 100;
            g_aiConfig.minWinRate = 0.4;
            g_aiConfig.minProfitFactor = 1.2;
            g_aiConfig.maxDrawdown = 20;
            g_aiConfig.lookbackPeriod = 1000;
            g_aiConfig.useGPU = true;
            g_aiConfig.threadCount = 8;
            break;
            
        case AI_MODE_RUN:
            // 运行模式 - 使用低配置
            g_aiConfig.populationSize = UseLowResourceMode ? 50 : 100;
            g_aiConfig.maxGenerations = UseLowResourceMode ? 100 : 500;
            g_aiConfig.mutationRate = 0.15;
            g_aiConfig.crossoverRate = 0.8;
            g_aiConfig.minTrades = UseLowResourceMode ? 10 : 50;
            g_aiConfig.minWinRate = 0.4;
            g_aiConfig.minProfitFactor = 1.2;
            g_aiConfig.maxDrawdown = 20;
            g_aiConfig.lookbackPeriod = UseLowResourceMode ? 100 : 500;
            g_aiConfig.useGPU = false;
            g_aiConfig.threadCount = UseLowResourceMode ? 2 : 4;
            break;
            
        case AI_MODE_HYBRID:
            // 混合模式 - 根据资源使用情况动态调整
            g_aiConfig.populationSize = UseLowResourceMode ? 100 : 300;
            g_aiConfig.maxGenerations = UseLowResourceMode ? 500 : 2000;
            g_aiConfig.mutationRate = 0.15;
            g_aiConfig.crossoverRate = 0.8;
            g_aiConfig.minTrades = UseLowResourceMode ? 20 : 100;
            g_aiConfig.minWinRate = 0.4;
            g_aiConfig.minProfitFactor = 1.2;
            g_aiConfig.maxDrawdown = 20;
            g_aiConfig.lookbackPeriod = UseLowResourceMode ? 200 : 800;
            g_aiConfig.useGPU = false;
            g_aiConfig.threadCount = UseLowResourceMode ? 2 : 6;
            break;
    }
    
    // 记录AI配置
    LogMessage(LOG_INFO, "=== AI配置信息 ===");
    LogMessage(LOG_INFO, StringFormat("种群大小: %d", g_aiConfig.populationSize));
    LogMessage(LOG_INFO, StringFormat("最大代数: %d", g_aiConfig.maxGenerations));
    LogMessage(LOG_INFO, StringFormat("回看周期: %d", g_aiConfig.lookbackPeriod));
    LogMessage(LOG_INFO, StringFormat("线程数: %d", g_aiConfig.threadCount));
}

//+------------------------------------------------------------------+
//| Expert deinitialization function
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
    LogMessage(LOG_INFO, "=== EA终止运行 ===");
    LogMessage(LOG_INFO, StringFormat("终止原因: %d", reason));
    
    // 释放指标句柄
    if(g_waveHandle != INVALID_HANDLE) {
        IndicatorRelease(g_waveHandle);
        g_waveHandle = INVALID_HANDLE;
    }
    
    if(g_gaHandle != INVALID_HANDLE) {
        IndicatorRelease(g_gaHandle);
        g_gaHandle = INVALID_HANDLE;
    }
    
    // 生成最终报告
    GenerateDetailedReport();
}

//+------------------------------------------------------------------+
//| Expert tick function
//+------------------------------------------------------------------+
void OnTick() {
    // 检查是否新K线
    CheckNewBar();
    
    // 更新市场状态
    UpdateMarketState();
    
    // 检查是否需要训练
    if(EnableAIOptimization && ShouldTrain()) {
        TrainModel();
    }
    
    // 波浪分析
    if(EnableWaveTheory && g_isNewBar) {
        AnalyzeWaves();
    }
    
    // 遗传算法优化
    if(EnableGA && g_isNewBar) {
        OptimizeParameters();
    }
    
    // 交易管理
    if(EnableTrading) {
        ManageTrades();
    }
}

//+------------------------------------------------------------------+
//| 检查新K线
//+------------------------------------------------------------------+
void CheckNewBar() {
    datetime currentBarTime = iTime(_Symbol, PERIOD_CURRENT, 0);
    g_isNewBar = (currentBarTime != g_lastBarTime);
    if(g_isNewBar) {
        g_lastBarTime = currentBarTime;
        LogMessage(LOG_DEBUG, StringFormat("新K线 - 时间: %s", TimeToString(currentBarTime)));
    }
}

//+------------------------------------------------------------------+
//| 更新市场状态
//+------------------------------------------------------------------+
void UpdateMarketState() {
    double volatility = CalculateVolatility();
    double trend = CalculateTrend();
    double volume = CalculateVolume();
    
    string state = "震荡";
    if(trend > 0.7) state = "强势上涨";
    else if(trend > 0.3) state = "上涨";
    else if(trend < -0.7) state = "强势下跌";
    else if(trend < -0.3) state = "下跌";
    
    LogMarketState(state, volatility, trend, volume);
}

//+------------------------------------------------------------------+
//| 分析波浪
//+------------------------------------------------------------------+
void AnalyzeWaves() {
    LogMessage(LOG_INFO, "=== 开始波浪分析 ===");
    
    // 获取价格数据
    double high[], low[], close[], volume[];
    ArraySetAsSeries(high, true);
    ArraySetAsSeries(low, true);
    ArraySetAsSeries(close, true);
    ArraySetAsSeries(volume, true);
    
    int copied = CopyHigh(_Symbol, PERIOD_CURRENT, 0, WaveLookback, high);
    if(copied != WaveLookback) {
        LogError("AnalyzeWaves", "复制最高价数据失败");
        return;
    }
    
    copied = CopyLow(_Symbol, PERIOD_CURRENT, 0, WaveLookback, low);
    if(copied != WaveLookback) {
        LogError("AnalyzeWaves", "复制最低价数据失败");
        return;
    }
    
    copied = CopyClose(_Symbol, PERIOD_CURRENT, 0, WaveLookback, close);
    if(copied != WaveLookback) {
        LogError("AnalyzeWaves", "复制收盘价数据失败");
        return;
    }
    
    copied = CopyTickVolume(_Symbol, PERIOD_CURRENT, 0, WaveLookback, volume);
    if(copied != WaveLookback) {
        LogError("AnalyzeWaves", "复制成交量数据失败");
        return;
    }
    
    // 执行波浪分析
    LogCompleteWaveAnalysis(0, high, low, close, volume);
}

//+------------------------------------------------------------------+
//| 优化参数
//+------------------------------------------------------------------+
void OptimizeParameters() {
    if(!EnableGA) return;
    
    LogMessage(LOG_INFO, "=== 开始参数优化 ===");
    
    // 获取优化数据
    double optimizationData[];
    ArraySetAsSeries(optimizationData, true);
    
    int copied = CopyBuffer(g_gaHandle, 0, 0, GAPopulationSize, optimizationData);
    if(copied != GAPopulationSize) {
        LogError("OptimizeParameters", "获取优化数据失败");
        return;
    }
    
    // 记录优化过程
    for(int i = 0; i < GAPopulationSize; i++) {
        LogGeneticAlgorithm(0, i, optimizationData[i], 0, 0, 0, 0, "优化中");
    }
}

//+------------------------------------------------------------------+
//| 管理交易
//+------------------------------------------------------------------+
void ManageTrades() {
    // 检查是否有新的交易信号
    if(CheckNewSignal()) {
        ExecuteTrade();
    }
    
    // 管理现有订单
    ManageOpenOrders();
}

//+------------------------------------------------------------------+
//| 统计连续同向K线数量
//+------------------------------------------------------------------+
int CountConsecutiveBars(const double &close[], bool isUp) {
    int count = 1;
    for(int i = 0; i < ArraySize(close) - 1; i++) {
        if((isUp && close[i] > close[i+1]) || (!isUp && close[i] < close[i+1])) {
            count++;
        } else {
            break;
        }
    }
    return count;
}

//+------------------------------------------------------------------+
//| 检查新信号
//+------------------------------------------------------------------+
bool CheckNewSignal() {
    if(!g_isNewBar) return false;
    
    // 获取信号数据
    double signalBuffer[];
    ArraySetAsSeries(signalBuffer, true);
    int copied = CopyBuffer(g_waveHandle, 0, 0, 1, signalBuffer);
    if(copied != 1) {
        LogError("CheckNewSignal", "获取信号数据失败");
        return false;
    }
    
    // 获取收盘价数据用于连续K线计数
    double close[20];
    ArraySetAsSeries(close, true);
    int closeCopied = CopyClose(_Symbol, PERIOD_CURRENT, 0, 20, close);
    if(closeCopied < MinConsecutiveBars) {
        LogError("CheckNewSignal", "收盘价数据不足");
        return false;
    }
    
    // 统计连续同向K线数
    int upCount = CountConsecutiveBars(close, true);
    int downCount = CountConsecutiveBars(close, false);
    
    // 只允许3+2n根同向K线且大于等于最小值时信号通过
    bool allowUp = (upCount >= MinConsecutiveBars) && ((upCount - 3) % 2 == 0);
    bool allowDown = (downCount >= MinConsecutiveBars) && ((downCount - 3) % 2 == 0);
    
    // 记录信号
    if(signalBuffer[0] > 0 && allowUp) {
        LogTradeSignal(true, SymbolInfoDouble(_Symbol, SYMBOL_ASK), 
                      CalculateStopLoss(true), CalculateTakeProfit(true),
                      StringFormat("五浪上涨, 连续阳线: %d", upCount), signalBuffer[0]);
        return true;
    }
    else if(signalBuffer[0] < 0 && allowDown) {
        LogTradeSignal(false, SymbolInfoDouble(_Symbol, SYMBOL_BID),
                      CalculateStopLoss(false), CalculateTakeProfit(false),
                      StringFormat("五浪下跌, 连续阴线: %d", downCount), MathAbs(signalBuffer[0]));
        return true;
    }
    
    // 不满足连续K线过滤条件时，记录日志
    if(signalBuffer[0] > 0 && !allowUp) {
        LogMessage(LOG_INFO, StringFormat("多头信号被连续K线过滤, 当前连续阳线: %d", upCount));
    }
    if(signalBuffer[0] < 0 && !allowDown) {
        LogMessage(LOG_INFO, StringFormat("空头信号被连续K线过滤, 当前连续阴线: %d", downCount));
    }
    
    return false;
}

//+------------------------------------------------------------------+
//| 执行交易
//+------------------------------------------------------------------+
void ExecuteTrade() {
    MqlTradeRequest request = {};
    MqlTradeResult result = {};
    
    // 设置交易请求
    request.action = TRADE_ACTION_DEAL;
    request.symbol = _Symbol;
    request.volume = LotSize;
    request.deviation = 10;
    request.magic = 123456;
    
    // 获取信号方向
    double signalBuffer[];
    ArraySetAsSeries(signalBuffer, true);
    CopyBuffer(g_waveHandle, 0, 0, 1, signalBuffer);
    
    if(signalBuffer[0] > 0) {
        request.type = ORDER_TYPE_BUY;
        request.price = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
        request.sl = CalculateStopLoss(true);
        request.tp = CalculateTakeProfit(true);
    }
    else {
        request.type = ORDER_TYPE_SELL;
        request.price = SymbolInfoDouble(_Symbol, SYMBOL_BID);
        request.sl = CalculateStopLoss(false);
        request.tp = CalculateTakeProfit(false);
    }
    
    // 发送交易请求
    if(!OrderSend(request, result)) {
        LogError("ExecuteTrade", "订单发送失败: " + IntegerToString(GetLastError()));
        return;
    }
    
    // 记录交易执行
    if(result.retcode == TRADE_RETCODE_DONE) {
        LogTradeExecution(result.order, signalBuffer[0] > 0, result.price,
                         LotSize, request.sl, request.tp);
    }
}

//+------------------------------------------------------------------+
//| 管理开仓订单
//+------------------------------------------------------------------+
void ManageOpenOrders() {
    for(int i = OrdersTotal() - 1; i >= 0; i--) {
        if(OrderSelect(i, SELECT_BY_POS)) {
            if(OrderSymbol() == _Symbol && OrderMagicNumber() == 123456) {
                // 检查是否需要移动止损
                if(UseTrailingStop) {
                    CheckTrailingStop();
                }
            }
        }
    }
}

//+------------------------------------------------------------------+
//| 检查追踪止损
//+------------------------------------------------------------------+
void CheckTrailingStop() {
    if(!UseTrailingStop) return;
    
    double currentPrice = OrderType() == ORDER_TYPE_BUY ? 
                         SymbolInfoDouble(_Symbol, SYMBOL_BID) :
                         SymbolInfoDouble(_Symbol, SYMBOL_ASK);
    
    double currentSL = OrderStopLoss();
    double newSL = 0;
    
    if(OrderType() == ORDER_TYPE_BUY) {
        newSL = currentPrice - TrailingStop * _Point;
        if(newSL > currentSL + TrailingStep * _Point) {
            ModifyOrder(newSL);
        }
    }
    else {
        newSL = currentPrice + TrailingStop * _Point;
        if(newSL < currentSL - TrailingStep * _Point) {
            ModifyOrder(newSL);
        }
    }
}

//+------------------------------------------------------------------+
//| 修改订单
//+------------------------------------------------------------------+
void ModifyOrder(double newSL) {
    MqlTradeRequest request = {};
    MqlTradeResult result = {};
    
    request.action = TRADE_ACTION_SLTP;
    request.symbol = _Symbol;
    request.sl = newSL;
    request.tp = OrderTakeProfit();
    request.order = OrderTicket();
    
    if(!OrderSend(request, result)) {
        LogError("ModifyOrder", "修改订单失败: " + IntegerToString(GetLastError()));
    }
}

//+------------------------------------------------------------------+
//| 计算止损价格
//+------------------------------------------------------------------+
double CalculateStopLoss(bool isBuy) {
    double price = isBuy ? SymbolInfoDouble(_Symbol, SYMBOL_ASK) :
                          SymbolInfoDouble(_Symbol, SYMBOL_BID);
    return isBuy ? price - StopLoss * _Point : price + StopLoss * _Point;
}

//+------------------------------------------------------------------+
//| 计算止盈价格
//+------------------------------------------------------------------+
double CalculateTakeProfit(bool isBuy) {
    double price = isBuy ? SymbolInfoDouble(_Symbol, SYMBOL_ASK) :
                          SymbolInfoDouble(_Symbol, SYMBOL_BID);
    return isBuy ? price + TakeProfit * _Point : price - TakeProfit * _Point;
}

//+------------------------------------------------------------------+
//| 计算波动率
//+------------------------------------------------------------------+
double CalculateVolatility() {
    double high[], low[];
    ArraySetAsSeries(high, true);
    ArraySetAsSeries(low, true);
    
    int copied = CopyHigh(_Symbol, PERIOD_CURRENT, 0, 20, high);
    if(copied != 20) return 0;
    
    copied = CopyLow(_Symbol, PERIOD_CURRENT, 0, 20, low);
    if(copied != 20) return 0;
    
    double sumRange = 0;
    for(int i = 0; i < 20; i++) {
        sumRange += high[i] - low[i];
    }
    
    return sumRange / 20.0;
}

//+------------------------------------------------------------------+
//| 计算趋势
//+------------------------------------------------------------------+
double CalculateTrend() {
    double close[];
    ArraySetAsSeries(close, true);
    
    int copied = CopyClose(_Symbol, PERIOD_CURRENT, 0, 20, close);
    if(copied != 20) return 0;
    
    double sumChange = 0;
    for(int i = 1; i < 20; i++) {
        sumChange += close[i-1] - close[i];
    }
    
    return sumChange / 19.0;
}

//+------------------------------------------------------------------+
//| 计算成交量
//+------------------------------------------------------------------+
double CalculateVolume() {
    double volume[];
    ArraySetAsSeries(volume, true);
    
    int copied = CopyTickVolume(_Symbol, PERIOD_CURRENT, 0, 20, volume);
    if(copied != 20) return 0;
    
    double sumVolume = 0;
    for(int i = 0; i < 20; i++) {
        sumVolume += volume[i];
    }
    
    return sumVolume / 20.0;
}

//+------------------------------------------------------------------+
//| 检查是否需要训练
//+------------------------------------------------------------------+
bool ShouldTrain() {
    if(!g_isNewBar) return false;
    
    // 检查训练间隔
    datetime currentTime = TimeCurrent();
    if(currentTime - g_lastTrainingTime < AITrainingInterval * 3600) return false;
    
    // 检查系统资源
    if(!CheckSystemResources()) return false;
    
    return true;
}

//+------------------------------------------------------------------+
//| 检查系统资源
//+------------------------------------------------------------------+
bool CheckSystemResources() {
    // 检查CPU使用率
    double cpuUsage = GetCPUUsage();
    if(cpuUsage > (UseLowResourceMode ? 50 : 80)) {
        LogMessage(LOG_WARNING, StringFormat("CPU使用率过高: %.1f%%", cpuUsage));
        return false;
    }
    
    // 检查内存使用率
    double memoryUsage = GetMemoryUsage();
    if(memoryUsage > (UseLowResourceMode ? 60 : 80)) {
        LogMessage(LOG_WARNING, StringFormat("内存使用率过高: %.1f%%", memoryUsage));
        return false;
    }
    
    return true;
}

//+------------------------------------------------------------------+
//| 训练模型
//+------------------------------------------------------------------+
void TrainModel() {
    if(g_isTraining) return;
    
    g_isTraining = true;
    LogMessage(LOG_INFO, "=== 开始模型训练 ===");
    
    // 保存当前配置
    AI_Config oldConfig = g_aiConfig;
    
    // 切换到训练模式配置
    g_aiConfig.populationSize = 500;
    g_aiConfig.maxGenerations = 5000;
    g_aiConfig.lookbackPeriod = 1000;
    g_aiConfig.threadCount = 8;
    
    // 执行训练
    OptimizeParameters();
    
    // 恢复原配置
    g_aiConfig = oldConfig;
    
    // 更新训练时间
    g_lastTrainingTime = TimeCurrent();
    g_isTraining = false;
    
    LogMessage(LOG_INFO, "=== 模型训练完成 ===");
}

//+------------------------------------------------------------------+
//| 获取CPU使用率
//+------------------------------------------------------------------+
double GetCPUUsage() {
    // 这里需要实现获取CPU使用率的代码
    // 可以使用Windows API或第三方库
    return 0.0;
}

//+------------------------------------------------------------------+
//| 获取内存使用率
//+------------------------------------------------------------------+
double GetMemoryUsage() {
    // 这里需要实现获取内存使用率的代码
    // 可以使用Windows API或第三方库
    return 0.0;
}

//+------------------------------------------------------------------+
//| 验证输入参数
//+------------------------------------------------------------------+
bool ValidateInputs() {
    if(LotSize <= 0) {
        LogError("ValidateInputs", "交易手数必须大于0");
        return false;
    }
    
    if(StopLoss <= 0) {
        LogError("ValidateInputs", "止损点数必须大于0");
        return false;
    }
    
    if(TakeProfit <= 0) {
        LogError("ValidateInputs", "止盈点数必须大于0");
        return false;
    }
    
    if(UseTrailingStop && TrailingStop <= 0) {
        LogError("ValidateInputs", "追踪止损点数必须大于0");
        return false;
    }
    
    if(UseTrailingStop && TrailingStep <= 0) {
        LogError("ValidateInputs", "追踪步长必须大于0");
        return false;
    }
    
    if(WaveLookback <= 0) {
        LogError("ValidateInputs", "波浪回看周期必须大于0");
        return false;
    }
    
    if(MinWaveAmplitude <= 0) {
        LogError("ValidateInputs", "最小波浪幅度必须大于0");
        return false;
    }
    
    if(MaxWaveRetracement <= 0 || MaxWaveRetracement >= 1) {
        LogError("ValidateInputs", "最大回撤比例必须在0-1之间");
        return false;
    }
    
    if(MinWaveExtension <= 0) {
        LogError("ValidateInputs", "最小延伸比例必须大于0");
        return false;
    }
    
    if(GAPopulationSize <= 0) {
        LogError("ValidateInputs", "种群大小必须大于0");
        return false;
    }
    
    if(GAGenerations <= 0) {
        LogError("ValidateInputs", "代数必须大于0");
        return false;
    }
    
    if(GAMutationRate < 0 || GAMutationRate > 1) {
        LogError("ValidateInputs", "变异率必须在0-1之间");
        return false;
    }
    
    if(GACrossoverRate < 0 || GACrossoverRate > 1) {
        LogError("ValidateInputs", "交叉率必须在0-1之间");
        return false;
    }
    
    if(GAMinTrades <= 0) {
        LogError("ValidateInputs", "最小交易次数必须大于0");
        return false;
    }
    
    if(GAMinWinRate < 0 || GAMinWinRate > 1) {
        LogError("ValidateInputs", "最小胜率必须在0-1之间");
        return false;
    }
    
    if(GAMinProfitFactor <= 0) {
        LogError("ValidateInputs", "最小盈亏比必须大于0");
        return false;
    }
    
    if(GAMaxDrawdown < 0 || GAMaxDrawdown > 100) {
        LogError("ValidateInputs", "最大回撤必须在0-100之间");
        return false;
    }
    
    return true;
} 